package com.iemes.controller.transport;


import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MotorcadeInfoFormMap;
import com.iemes.entity.TransportDriverinfoFormMap;
import com.iemes.entity.TransportrefuelFormMap;
import com.iemes.mapper.motorcade.MotorcadeMapper;
import com.iemes.mapper.transport.TransportRefuelMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
@RequestMapping("/transportrefuel/")
@Controller
public class TransportRefuelController extends BaseController{
	
	@Inject
	private TransportRefuelMapper transportrefuelMapper;
	@Inject
	private MotorcadeMapper motorcademapper;
	//@Inject
	//private TransportDriverinfoMapper transportDriverinfoMapper;
	
	//加油列表
	@RequestMapping("/examinetransportrefuel")
	public String examineTransport(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH+"/transport/rtrefuel/transportrefuel_list";
	}
	@ResponseBody
	@RequestMapping("/transportrefuel_findbyPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		TransportrefuelFormMap transportrefuelFormMap=getFormMap(TransportrefuelFormMap.class);
		List<Map<String, Object>> transportrefuelFormMaps = transportrefuelMapper.getAllInfo(transportrefuelFormMap);
		for (Map<String, Object> map : transportrefuelFormMaps) {
			map.put("totals", Common.convertSourData(map.get("refuelNow").toString())-Common.convertSourData((map.get("kilometreNumber").toString())));
			List<Map<String, Object>> transpor=transportrefuelMapper.getAll(Integer.valueOf(map.get("id").toString()));
			map.put("transpor", transpor);
		}
		
		LuogicPaging(pageNow, pageSize, transportrefuelFormMaps);
		return pageView;
	}
	
	
	//修改加油
	@RequestMapping("/transportrefuel_edit")
	public String tranup(Model model){
		TransportrefuelFormMap transportrefuelFormMap=getFormMap(TransportrefuelFormMap.class);
		
		String id = getPara("id");
		List<Map<String, Object>> transportrefuelFormMaps=transportrefuelMapper.getAllInfo(transportrefuelFormMap);

		//获取对象
		if(Common.isNotEmpty(id))
		{
			transportrefuelFormMap= baseMapper.findbyFrist("id", id, TransportrefuelFormMap.class);
			TransportDriverinfoFormMap transportDriverinfoFormMap=baseMapper.findbyFrist("id", id,TransportDriverinfoFormMap.class);
			for (Map<String, Object> map : transportrefuelFormMaps) {
				if(map.get("id")==transportrefuelFormMap.get("id")){
					transportrefuelFormMap.put("licenseNo", map.get("licenseNo"));
					transportrefuelFormMap.put("carBossName", map.get("carBossName"));
					transportrefuelFormMap.put("carBossNumber", map.get("carBossNumber"));
					transportrefuelFormMap.put("driverName", map.get("driverName"));
					transportrefuelFormMap.put("driverNumber", map.get("driverNumber"));
				}
			}
			model.addAttribute("tranedit", transportrefuelFormMap);
			model.addAttribute("trand", transportDriverinfoFormMap);
		}
		return Common.BACKGROUND_PATH + "/transport/rtrefuel/transportrefuel_edit";	
	}
	
	@ResponseBody
	@RequestMapping(value="transportrefuel_editEntity")
	@SystemLog(module="车队加油",methods="加油-编辑加油")//凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional
	public String editEntity() {
		TransportrefuelFormMap transportrefuelFormMap=getFormMap(TransportrefuelFormMap.class);
		String tlicenseNo=transportrefuelFormMap.getStr("tlicenseNo");
		MotorcadeInfoFormMap motorcadeInfoFormMap=motorcademapper.findbyFrist("licenseNo", tlicenseNo, MotorcadeInfoFormMap.class);
		//String id= motorcadeInfoFormMap.getStr("did");
		
		//String did=motorcadeInfoFormMap.getStr("did");
		//TransportDriverinfoFormMap transportDriverinfoFormMap=transportDriverinfoMapper.findbyFrist("id", id, TransportDriverinfoFormMap.class);
		//TransportDriverinfoFormMap transportDriverinfoFormMap=getFormMap(TransportDriverinfoFormMap.class);

		if(tlicenseNo.equals(transportrefuelFormMap.getStr("licenseNo"))){
			motorcadeInfoFormMap.put("carBossName", transportrefuelFormMap.get("carBossName"));
			motorcadeInfoFormMap.put("carBossNumber", transportrefuelFormMap.get("carBossNumber"));
			/*if (transportDriverinfoFormMap.put("id", motorcadeInfoFormMap.get("driverId")) != null) {
				transportDriverinfoFormMap.put("driverName", transportrefuelFormMap.get("motorcadeInfoFormMap.driverName"));
				transportDriverinfoFormMap.put("driverNumber",  transportrefuelFormMap.get("motorcadeInfoFormMap.driverNumber"));
				
			}*/
		}//transportDriverinfoFormMap.put("driverName", transportrefuelFormMap.get("transportrefuelFormMap.driverName"));
		//transportDriverinfoFormMap.put("driverNumber",  transportrefuelFormMap.get("transportrefuelFormMap.driverNumber"));
		
		//计算行驶公里数
		try {
			if (Common.convertSourData(transportrefuelFormMap.get("refuelNow").toString())>=
				Common.convertSourData((transportrefuelFormMap.get("kilometreNumber").toString()))
				&&(Common.convertSourData(transportrefuelFormMap.get("refuelNow").toString())>=0)
				&&(Common.convertSourData(transportrefuelFormMap.get("kilometreNumber").toString())>=0)
				) {
				transportrefuelFormMap.put("kilometrCount", Common.convertSourData(transportrefuelFormMap.get("refuelNow").toString())-Common.convertSourData((transportrefuelFormMap.get("kilometreNumber").toString())));

			}else{
				return "fasle";
			}
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		
		try {
			transportrefuelMapper.editEntity(transportrefuelFormMap);
			motorcademapper.editEntity(motorcadeInfoFormMap);
			//transportDriverinfoMapper.editEntity(transportDriverinfoFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fasle";
		}
		return "success";
	}
	/*//添加加油
	@RequestMapping("/transport_add")
	public String addUI(Model model){
		List<MotorcadeInfoFormMap> motorcadeInfoFormMap=motorcademapper.findByWhere(new MotorcadeInfoFormMap());
		model.addAttribute("motorcadeInfoFormMap", motorcadeInfoFormMap);
		return Common.BACKGROUND_PATH + "/transport/rtrefuel/transportrefuel_add";
	}*/
	
	/*@RequestMapping("/transportrefuel_id")
	@ResponseBody
	public MotorcadeInfoFormMap tmid(){
		String id=getPara("id");
		MotorcadeInfoFormMap motorcadeInfoFormMap=motorcademapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
		List<Map<String, Object>> motorcadeInfoFormMaps = motorcademapper.getAllInfo(motorcadeInfoFormMap);
		for (Map<String, Object> map : motorcadeInfoFormMaps) {
			List<Map<String, Object>> transpor=transportrefuelMapper.getAll(Integer.valueOf(map.get("id").toString()));
			map.put("transpor", transpor);
		}
		return motorcadeInfoFormMap;
	}*/

	
	/*@ResponseBody
	@RequestMapping("/transport_addEntity")
	@SystemLog(module="车队加油",methods="加油-新增加油")
	public String addEntity(){
		TransportrefuelFormMap transportrefuelFormMap=getFormMap(TransportrefuelFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		transportrefuelFormMap.put("byuser_id", userFormMap.getInt("id"));
		transportrefuelFormMap.set("signDate", DateUtils.getStringDateTime());
		try {
			if (Common.convertSourData(transportrefuelFormMap.get("refuelNow").toString())>=
					Common.convertSourData((transportrefuelFormMap.get("kilometreNumber").toString()))
					&&(Common.convertSourData(transportrefuelFormMap.get("refuelNow").toString())>=0)
					&&(Common.convertSourData(transportrefuelFormMap.get("kilometreNumber").toString())>=0)
					) {
					transportrefuelFormMap.put("kilometrCount", Common.convertSourData(transportrefuelFormMap.get("refuelNow").toString())-Common.convertSourData((transportrefuelFormMap.get("kilometreNumber").toString())));

				}else{
					return "fasle";
				}
			transportrefuelMapper.addEntity(transportrefuelFormMap);
		} catch (Exception e) {
			return "false";
		}
		return "success";
	}*/
	//删除加油
	@ResponseBody
	@RequestMapping("transportrefuel_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="删除加油",methods="pod按钮-删除pod按钮")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		TransportrefuelFormMap transportrefuelFormMap=getFormMap(TransportrefuelFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			transportrefuelFormMap.set("status", 2);
			transportrefuelFormMap.put("id", str[i]);
			transportrefuelMapper.editEntity(transportrefuelFormMap);
		}
		
		return "success";
	}
}
